package com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzxczh.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.vo.KnoBaseExcelVo;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.service.PersonnelManagementService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.vo.ImportExcelVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.zcpt.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Component
public class KnoBaseVerify implements IExcelVerifyHandler<KnoBaseExcelVo> {

    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    DictService dictService;
    @Autowired
    EarEveManageService earEveManageService;
    @Autowired
    PersonnelManagementService personnelManagementService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(KnoBaseExcelVo importExcelVo) {

        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");


        if (PlatformObjectUtils.isEmpty(importExcelVo.getKnowledgeName())) {
            result.setSuccess(false);
            message.append("名称 不能为空！");
        }


        if (PlatformObjectUtils.isEmpty(importExcelVo.getCategory())) {
            result.setSuccess(false);
            message.append("知识库类别 不能为空！");
        } else {
            //校验导入的数据是否规范,导入的值数据字典中是否存在
            String structureType = importExcelVo.getCategory();
            List<String> list = checkData("zsk");
            if (!list.contains(structureType)) {
                result.setSuccess(false);
                message.append("知识库类别 不正确！");
            }
        }

        if (PlatformObjectUtils.isNotEmpty(importExcelVo.getCommentInfo())) {
            if (importExcelVo.getCommentInfo().length() > 1000) {
                result.setSuccess(false);
                message.append("描述信息 不能超过1000字！");
            }
        }


        result.setMsg(message.toString());
        return result;
    }

    private List<String> checkData(String code) {
        List<DictItemEntity> validDictItemsByDictCode = dictService.getValidDictItemsByDictCode(code);
        List<String> list = new ArrayList<>();
        for (DictItemEntity dictItemEntity : validDictItemsByDictCode) {
            String dictItemName = dictItemEntity.getDictItemCode();
            list.add(dictItemName);
        }
        return list;
    }


}
